package de.openkanban.client.ui.footer;

import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.util.EventHandler;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.events.DropEvent;
import com.smartgwt.client.widgets.events.DropHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.tile.TileGrid;
import com.smartgwt.client.widgets.tile.TileRecord;

import de.openkanban.client.datasource.BoardContactDS;
import de.openkanban.client.ui.board.BoardComposite;
import de.openkanban.client.ui.presenter.AddContactToBoardRequestCallback;
import de.openkanban.client.ui.presenter.DeleteBoardPerformerCallback;
import de.openkanban.client.ui.presenter.PreparedRequest;
import de.openkanban.client.util.RootPanel;
import de.openkanban.client.util.StringUtils;

/**
 * GUI Element for the BoardContactTileGrid.
 * Part of the BoardComposite so it must react if board switches.
 */
public class BoardContactTileGrid extends TileGrid implements BoardComposite {

	public BoardContactTileGrid() {
		setWidth100();
		setHeight(146);
		setTileWidth(50);
		setTileHeight(58);
		setCanAcceptDrop(true);
		setPreventDuplicates(true);
		setAutoFetchData(false);
		setSmoothFade(true);
		setStyleName("boardContactTileGrid");
		setMargin(0);
		setPadding(0);
		setTileVMargin(1);
		setTileHMargin(3);
		setDragDataAction(DragDataAction.NONE);
		setCanDragTilesOut(true);
		setDropTypes("board_contact");

		addDropHandler(new DropHandler() {
			public void onDrop(DropEvent event) {

				Long currentBoardId = RootPanel.main.getBoard().getWhiteBoardView().getBoardDto().getBoardId();

				Canvas target = EventHandler.getDragTarget();
				if (target instanceof ListGrid) {
					ListGrid dragListGird = (ListGrid) target;
					Record dragRecord = dragListGird.getDragData()[0];
					String userId = dragRecord.getAttribute("userId");
					if (StringUtils.isNumeric(userId) && currentBoardId != null && currentBoardId > 0) {
						PreparedRequest prepReq = new PreparedRequest("board", currentBoardId + "/contact/add/"
								+ userId);
						prepReq.sendAsyncRequest(new AddContactToBoardRequestCallback());
					}
				}
			}
		});

		addDoubleClickHandler(new DoubleClickHandler() {

			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				BoardContactTileGrid bctg = (BoardContactTileGrid) event.getSource();
				TileRecord tr = bctg.getSelectedRecord();
				if (tr != null) {
					String vorname = tr.getAttribute("vorname");
					String nachname = tr.getAttribute("nachname");
					SC.confirm("Do you want to remove " + vorname + " " + nachname + " from Board?",
							new DeleteBoardPerformerCallback(event.getSource()));
				}
			}
		});

		RootPanel.boardComponent.addComposite(this);
	}

	@Override
	public void switchBoard(Long boardId) {
		if (getDataSource() != null) {
			getDataSource().destroy();
			BoardContactDS.setInstanceNull();
		}
		setDataSource(BoardContactDS.getInstance(boardId.toString()));
		fetchData();
		filterData();
		redraw();
	}
}


